(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.incomingRequest.viewController', viewController);
	viewController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'incomingRequestService' ];
	function viewController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, incomingRequestService) {
		var ctrl = this, 
			log = $scope.loggerFactory.getLogger("incomingRequestView"),
			errorMessage = {}, 
			params = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			authService.view(params).then(function(response) {
				if (response.data) {
					ctrl.incomingRequest = response.data;
				}
			});
		};
		
		ctrl.view = function (){
			modalService.openCustomDialogs({
			   'title': 'Response Messages',
			   'dialogsModule' : 'cda.incomingRequest.viewResponse',
			   'params': ctrl.incomingRequest,
			   'btnCallback' : {
					'ok' : function(resultList) {
					}
				}	
			});
		};
		
		ctrl.submit = function() {
			
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			return authService.process(ctrl.selectedRows[0]).then(submitSuccessful, submitFailed);
			
		};	
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				},{
					'name' : 'menu.incomingRequest.list',
					'state' : 'cda.incomingRequest.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : [ 'error appeared: ' + angular.toJson(response.error) ]
			});
		}
	}
})(angular);
